/*
 * PartCommonFilterJPanel.java
 *
 * Created on 05.04.2009, 20:46:32
 */
package morenoapp.components.thinkgraph;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;
import morenoapp.components.thinkgraph.model.DisplayFilter;
import morenoapp.util.I18N;
import org.jdesktop.application.Action;

/**
 * 
 * @author dmitry.mamonov
 */
public class PartCommonFilterJPanel extends javax.swing.JPanel {

    private DisplayFilter filter;

    /** Creates new form PartCommonFilterJPanel */
    public PartCommonFilterJPanel(DisplayFilter _filter) {
        this.filter = _filter;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jFileChooserSaveFile = new javax.swing.JFileChooser();
        jCheckBoxAccurateDraw = new javax.swing.JCheckBox();
        jCheckBoxDisplayTooltips = new javax.swing.JCheckBox();
        jSliderTargetStep = new javax.swing.JSlider();
        jLabelTargetStep = new javax.swing.JLabel();
        jComboBoxView = new javax.swing.JComboBox();
        jLabelView = new javax.swing.JLabel();
        jButtonAddView = new javax.swing.JButton();
        jButtonRemoveView = new javax.swing.JButton();
        jButtonSaveAsImage = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(morenoapp.MorenoApp.class).getContext().getResourceMap(PartCommonFilterJPanel.class);
        jFileChooserSaveFile.setDialogTitle(resourceMap.getString("jFileChooserSaveFile.dialogTitle")); // NOI18N
        jFileChooserSaveFile.setFileFilter(getFileFilter());
        jFileChooserSaveFile.setName("jFileChooserSaveFile"); // NOI18N
        jFileChooserSaveFile.setOpaque(true);

        setName("Form"); // NOI18N

        jCheckBoxAccurateDraw.setText(resourceMap.getString("jCheckBoxAccurateDraw.text")); // NOI18N
        jCheckBoxAccurateDraw.setName("jCheckBoxAccurateDraw"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${filter.accurateDraw}"), jCheckBoxAccurateDraw, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        jCheckBoxDisplayTooltips.setText(resourceMap.getString("jCheckBoxDisplayTooltips.text")); // NOI18N
        jCheckBoxDisplayTooltips.setName("jCheckBoxDisplayTooltips"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${filter.displayTooltips}"), jCheckBoxDisplayTooltips, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        jSliderTargetStep.setMajorTickSpacing(100);
        jSliderTargetStep.setMaximum(220);
        jSliderTargetStep.setMinimum(20);
        jSliderTargetStep.setMinorTickSpacing(20);
        jSliderTargetStep.setPaintLabels(true);
        jSliderTargetStep.setPaintTicks(true);
        jSliderTargetStep.setName("jSliderTargetStep"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${filter.targetStep}"), jSliderTargetStep, org.jdesktop.beansbinding.BeanProperty.create("value"));
        bindingGroup.addBinding(binding);

        jLabelTargetStep.setText(resourceMap.getString("jLabelTargetStep.text")); // NOI18N
        jLabelTargetStep.setName("jLabelTargetStep"); // NOI18N

        jComboBoxView.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "NOI18N Основной вид", "NOI18N Item 2", "NOI18N Item 3", "NOI18N Item 4" }));
        jComboBoxView.setName("jComboBoxView"); // NOI18N

        jLabelView.setText(resourceMap.getString("jLabelView.text")); // NOI18N
        jLabelView.setName("jLabelView"); // NOI18N

        jButtonAddView.setIcon(resourceMap.getIcon("jButtonAddView.icon")); // NOI18N
        jButtonAddView.setText(resourceMap.getString("jButtonAddView.text")); // NOI18N
        jButtonAddView.setToolTipText(resourceMap.getString("jButtonAddView.toolTipText")); // NOI18N
        jButtonAddView.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonAddView.setIconTextGap(0);
        jButtonAddView.setName("jButtonAddView"); // NOI18N

        jButtonRemoveView.setIcon(resourceMap.getIcon("jButtonRemoveView.icon")); // NOI18N
        jButtonRemoveView.setToolTipText(resourceMap.getString("jButtonRemoveView.toolTipText")); // NOI18N
        jButtonRemoveView.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonRemoveView.setIconTextGap(0);
        jButtonRemoveView.setName("jButtonRemoveView"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(morenoapp.MorenoApp.class).getContext().getActionMap(PartCommonFilterJPanel.class, this);
        jButtonSaveAsImage.setAction(actionMap.get("actionSaveAsImage")); // NOI18N
        jButtonSaveAsImage.setIcon(resourceMap.getIcon("jButtonSaveAsImage.icon")); // NOI18N
        jButtonSaveAsImage.setToolTipText(resourceMap.getString("jButtonSaveAsImage.toolTipText")); // NOI18N
        jButtonSaveAsImage.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonSaveAsImage.setIconTextGap(0);
        jButtonSaveAsImage.setName("jButtonSaveAsImage"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonSaveAsImage, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelView)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxView, 0, 88, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonAddView, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonRemoveView, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jCheckBoxAccurateDraw, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBoxDisplayTooltips)
                    .addComponent(jLabelTargetStep)
                    .addComponent(jSliderTargetStep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(13, 13, 13)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jComboBoxView, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelView))
                                .addGap(3, 3, 3))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jButtonRemoveView, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButtonAddView, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGap(5, 5, 5))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonSaveAsImage, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(5, 5, 5)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxAccurateDraw)
                .addGap(3, 3, 3)
                .addComponent(jCheckBoxDisplayTooltips)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelTargetStep)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSliderTargetStep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAddView;
    private javax.swing.JButton jButtonRemoveView;
    private javax.swing.JButton jButtonSaveAsImage;
    private javax.swing.JCheckBox jCheckBoxAccurateDraw;
    private javax.swing.JCheckBox jCheckBoxDisplayTooltips;
    private javax.swing.JComboBox jComboBoxView;
    private javax.swing.JFileChooser jFileChooserSaveFile;
    private javax.swing.JLabel jLabelTargetStep;
    private javax.swing.JLabel jLabelView;
    private javax.swing.JSlider jSliderTargetStep;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public DisplayFilter getFilter() {
        return filter;
    }

    public FileFilter getFileFilter() {
        return new FileFilter() {

            @Override
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                }

                return f.getName().toLowerCase().endsWith(".png"); //NOI18N
            }

            @Override
            public String getDescription() {
                return I18N.get("label.png_is_standard_image_export_format_for_bulleye");
            }
        };
    }

    @Action
    public void actionSaveAsImage() throws IOException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd  HH-mm-ss"); //NOI18N
        String targetFileName = filter.getFilterName()+" ("+dateFormat.format(new Date()) + ").png"; //NOI18N
        jFileChooserSaveFile.setCurrentDirectory(new File(".")); //NOI18N
        jFileChooserSaveFile.setSelectedFile(new File(targetFileName));
        
        int returnVal = jFileChooserSaveFile.showDialog(SwingUtilities.getRoot(this), I18N.get("label.save"));
        if (returnVal==JFileChooser.APPROVE_OPTION){
            File targetFile = jFileChooserSaveFile.getSelectedFile();
            if(!targetFile.getName().toLowerCase().endsWith(".png")){ //NOI18N
                targetFile = new File(targetFile.getAbsolutePath()+".png"); //NOI18N
            }

            if (targetFile.exists()){
                int optionVal = JOptionPane.showConfirmDialog(SwingUtilities.getRoot(this), I18N.get("label.confirm_file_overwrite")+": "+targetFile.getName()+"?");
                if(JOptionPane.YES_OPTION!=optionVal){
                    return;
                }
            }
            filter.getGraph().saveAsImage(targetFile);
        }
    }
}
